home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Encarta World Atlas 1999 / Encarta World 99 Install Disk.iso / evg / Flights / SoundsFn.js < prev    next >
Encoding:
JavaScript  |  1998-09-01  |  7.3 KB  |  267 lines

  1. /*****************************************************************************
  2.  SoundsFn.js
  3.  
  4.  Encarta Virtual Globe 1999
  5.  
  6.  (c) Copyright Microsoft Corporation 1998
  7.  
  8.  Fly Through has site specific sounds/music. This include file contains the 
  9.  functions which return which
  10.  index to use in the media source nodes of the media graph. It is assumed
  11.  that the data tables here are in sync with the MediaGraph control nodes,
  12.  that contain the paths to the proper music. If one changes, make sure the
  13.  other does as well. The media graph can be altered using AudioMan's
  14.  MediaAuthor and the Sounds.mgd file. Cut and paste the control specification
  15.  in to the TerrainCtl.htm file.
  16.  
  17.  This file is to be included by Fly*.htm 
  18.  
  19.  Owner = WHsu
  20. *****************************************************************************/
  21.  
  22. function LoadSounds()
  23. {
  24.     g_WindSoundNode      = MediaGraph.Nodes("WindSound");
  25.     g_RadioSoundsNode     = MediaGraph.Nodes("RadioSounds");
  26.  
  27.     LoadFusionMedia();
  28.     MediaGraph.Nodes("WindSound").LoadMedia(false);
  29. }
  30.  
  31. function PlaySound()
  32. {
  33.     // Decide which sound to play. Make sure this is only called when safe (OnLoad and OnStop)
  34.     PlayIndex = g_PlayIndex;        // inc before play in case Play* calls PlaySound again
  35.     g_PlayIndex = (g_PlayIndex+1) % g_PlayList.length;
  36.     eval( g_PlayList[PlayIndex] );
  37. //    G_DEBUG_PLAY_COUNT++;
  38. //    window.status = "Play Index = " + g_PlayIndex + " count = " + G_DEBUG_PLAY_COUNT;          // TEMP, FOR DEBUGGING
  39. }
  40.  
  41. function PlayWind()
  42. {
  43.     if ( MediaGraph.Nodes("WindSound").IsAvailable )
  44.     {
  45.         MediaGraph.Nodes("WindChannel").Play();
  46.     } else {
  47.         PlaySound();
  48.         MediaGraph.Nodes("WindSound").LoadMedia(false);
  49.     }
  50. }
  51.  
  52. function PlayFusion()
  53. {
  54.     // Might do some random selection of which fusion source we want to use later
  55.     if ( g_fFusionSrcAllLoaded )
  56.     {
  57.         PlayFusionNow();
  58.     } else {
  59.         PlaySound();
  60.         LoadFusionMedia();
  61.     }
  62. }
  63.  
  64. function PlayFusionNow()
  65. {
  66.     MediaGraph.Nodes("FusionChannel").Play()
  67. }
  68.  
  69. function PlaySiteSound()
  70. {
  71.  
  72.     // find the nearest site sound
  73.     var Lat, Long, index;
  74.     Lat = TerrainCtl.PositionLat;
  75.     Long = TerrainCtl.PositionLong;
  76.     index = FindNearestSound( Lat, Long, g_Range[g_Continent] );
  77.     if ( index != 0 )
  78.     {
  79.         MediaGraph.Nodes("RadioSounds").LoadMedia(false);
  80.         MediaGraph.Nodes(g_SiteSounds).Index = index;
  81.         MediaGraph.Nodes(g_SiteSounds).LoadMedia(false);
  82.     }
  83.     else
  84.     {
  85. //        alert( "DEBUG Nothing interesting around, skipping to next sound " + g_Range[g_Continent] );
  86.         PlaySound();    // Nothing interesting around, go on to next sound
  87.     }
  88. }
  89.  
  90. function PlaySiteSoundNow()
  91. {
  92.     // since the source of our sounds are specified with an URL, they
  93.     // are downloaded asynchronously, and we need to wait until we are 
  94.     // notified that we can play it. Otherwise, script error.
  95.     MediaGraph.Nodes("SiteChannel").Play();
  96. }
  97.  
  98. // NOTE: This function is called from the Shell through Options dialog
  99. function SoundsOn()
  100. {
  101.     if ( g_fFusionSrcAllLoaded )
  102.     {
  103.         g_fSoundsEnabled = true;
  104.         ResumeSounds();
  105.     } else if ( ((null == window.external.GetPackedSettings) || 
  106.                  (window.external.GetPackedSettings() & 0x00000004)) &&
  107.                 MediaGraph.OutputDeviceAvailable ) {
  108.         g_fSoundsEnabled = true;
  109.         LoadSounds();
  110.     }
  111. }
  112.  
  113. // NOTE: This function is called from the Shell through Options dialog
  114. function SoundsOff()
  115. {
  116.     if ( g_fSoundsEnabled )
  117.     {
  118.         PauseSounds();
  119.     }
  120.     g_fSoundsEnabled = false;
  121. }
  122.  
  123. function StopSounds()
  124. {
  125.     // stop sounds and reset the play list
  126.     MediaGraph.Nodes("WindChannel").Stop();
  127.     MediaGraph.Nodes("SiteChannel").Stop();
  128.     MediaGraph.Nodes("FusionChannel").Stop();
  129.     g_PlayIndex = 0;
  130. }
  131.  
  132. function PauseSounds()
  133. {
  134.     if ( g_fSoundsActive )
  135.     {
  136.         // PlayState of 1 means that it is currently playing
  137.         if( 1 == MediaGraph.Nodes("WindChannel").PlayState )
  138.         {
  139.             MediaGraph.Nodes("WindChannel").Pause();
  140.         }
  141.         if( 1 == MediaGraph.Nodes("SiteChannel").PlayState )
  142.         {
  143.         MediaGraph.Nodes("SiteChannel").Pause();
  144.         }
  145.         if( 1 == MediaGraph.Nodes("FusionChannel").PlayState )
  146.         {
  147.         MediaGraph.Nodes("FusionChannel").Pause();
  148.         }
  149.     }
  150. }
  151.  
  152. function ResumeSounds()
  153. {
  154.     if ( g_fSoundsEnabled && g_fSoundsActive )
  155.     {
  156.         // PlayState of 2 means that it is currently Paused
  157.         if( 2 == MediaGraph.Nodes("WindChannel").PlayState )
  158.         {
  159.             MediaGraph.Nodes("WindChannel").Play();
  160.         }
  161.         if( 2 == MediaGraph.Nodes("SiteChannel").PlayState )
  162.         {
  163.             MediaGraph.Nodes("SiteChannel").Play();
  164.         }
  165.         if( 2 == MediaGraph.Nodes("FusionChannel").PlayState )
  166.         {
  167.             MediaGraph.Nodes("FusionChannel").Play();
  168.         }
  169.     }
  170. }
  171.  
  172. function SetContinent( Continent )
  173. {
  174.     for ( i=0; i<g_Continent_Num; i++ )
  175.     {
  176.         if ( Continent == g_Continent_Names[i] )
  177.         {
  178.             if ( g_fSoundsActive )
  179.             {
  180.                 StopSounds();
  181.             }
  182.             g_Continent = i;
  183.             g_SiteSounds = "SiteSounds" + g_Continent_Names[g_Continent];
  184.             g_SiteSoundsNode = MediaGraph.Nodes(g_SiteSounds);
  185.             if ( g_fSoundsActive )
  186.             {
  187.                 PlaySound();
  188.             }
  189.             return;
  190.         }
  191.     }
  192. //    alert( "DEBUG Attemping to set World Flight Continent to an undefined value " + g_Continent + ", " + Continent );
  193. }
  194.  
  195. // Returns the index of the sound that is closest to the given lat, long.
  196. // If a (Lat/Long distance) Range is specified, a sound within that
  197. // range is randomly chosen. If no sound is within Range, a null 
  198. // string is returned. 
  199. // If Range is less than or equal to zero, the nearest sounds is returned
  200. function FindNearestSound( Lat, Long, Range )
  201. {
  202.     if ( Range <= 0 ) 
  203.     {
  204.         return FindAnyNearestSound( Lat, Long );
  205.     }
  206.  
  207.     var y, x, distance;
  208.     var index = 0;
  209.     var hits = new Array();
  210.     var sum = 0.0;
  211.  
  212.     // maintain a list of all hits within range and randomly 
  213.     // select amoung those. hits are weighted relative to their
  214.     // distance from the ship
  215.     Range *= Range;
  216.     for ( i=0; i < g_SoundTable[g_Continent].length; i++ )
  217.     {
  218.         y = Lat - g_SoundTable[g_Continent][i][g_indexLat];
  219.         x = Long - g_SoundTable[g_Continent][i][g_indexLong];
  220.         distance = (x*x) + (y*y);
  221.         if ( distance < Range )
  222.         {
  223.             hits[index] = new Array(2);
  224.             hits[index][0] = Range/distance;
  225.             sum += hits[index][0];
  226.             hits[index][1] = i;
  227.             index++;
  228.         }
  229.     }
  230.     var ran = Math.random();
  231.  
  232.     // While we test each hit, we increment the next hit's value 
  233.     // so hits[*][0]/sum covers the range from 0 to 1.
  234.     for ( i=0; i < index; i++, hits[i][0]+=hits[i-1][0] )
  235.     {
  236.         if ( ran <= (hits[i][0]/sum) )
  237.         {
  238.             return g_SoundTable[g_Continent][hits[i][1]][g_indexSound];
  239.         }
  240.     }
  241.     return 0;
  242. }
  243.  
  244. // Returns the index of the sound that is closest to the given lat, long.
  245. function FindAnyNearestSound( Lat, Long )
  246. {
  247.     var y = Lat - g_SoundTable[g_Continent][0][g_indexLat];
  248.     var x = Long - g_SoundTable[g_Continent][0][g_indexLong];
  249.     var distance = (x*x) + (y*y);
  250.     var dist;
  251.     var index = 0;
  252.  
  253.     for ( i=1; i < g_SoundTable[g_Continent].length; i++ )
  254.     {
  255.         y = Lat - g_SoundTable[g_Continent][i][g_indexLat];
  256.         x = Long - g_SoundTable[g_Continent][i][g_indexLong];
  257.         dist = (x*x) + (y*y);
  258.         if ( dist < distance )
  259.         {
  260.             distance = dist;
  261.             index = i;
  262.         }
  263.     }
  264.     return g_SoundTable[g_Continent][index][g_indexSound];
  265. }
  266.  
  267.